package com.sxl.chapter.three;


public class FinalizeEscapeGC {

	public static FinalizeEscapeGC SAVE_HOOK = null;
	
	public void isAlive() {
		System.out.println("yes, i am still alive : )");
	}
	
	protected void finalize() throws Throwable {
		super.finalize();
		System.out.println("finalize method executed!");
		FinalizeEscapeGC.SAVE_HOOK = this;
	}
	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub
		SAVE_HOOK = new FinalizeEscapeGC();
		
		SAVE_HOOK = null;
		
		System.gc();
		Thread.sleep(2000);
//		
//		if (SAVE_HOOK != null ) {
//			SAVE_HOOK.isAlive();
//		} else {
//			System.out.println("NO,I AM DEAD : (");
//		}
//		
//		SAVE_HOOK = null;
		System.gc();
		Thread.sleep(2000);
		
		if (SAVE_HOOK != null ) {
			SAVE_HOOK.isAlive();
		} else {
			System.out.println("NO,I AM DEAD : (");
		}
	}

}
